#include "../../mqserver/exchange.hpp"
#include "../../mqcommon/msg.pb.h"
#include <gtest/gtest.h>

std::shared_ptr<ExchangeManager> managerPtr;

class ExchangeTest : public testing::Environment
{
public:
    virtual void SetUp()
    {
        managerPtr = std::make_shared<ExchangeManager>("./ccc/data.db");
    }
    virtual void TearDown()
    {
        managerPtr->clear();
    }
};

TEST(ExchangeTest, declareExchangeTest)
{
    std::unordered_map<std::string, std::string> mp = {{"k1", "v1"},{"k2", "v2"}};
    // for(auto&pair : mp)      //难怪，初始化就是反的
    // {
    //     DLOG("%s:%s",pair.first.c_str(), pair.second.c_str());
    // }
    managerPtr->declareExchange("exchange1", ExchangeType::DIRECT, true, false, mp);
    managerPtr->declareExchange("exchange2", ExchangeType::DIRECT, true, false, mp);
    managerPtr->declareExchange("exchange3", ExchangeType::DIRECT, true, false, mp);
    managerPtr->declareExchange("exchange3", ExchangeType::DIRECT, true, false, mp);    //重复插入不报错,因为在declareExchange过滤掉了
    managerPtr->declareExchange("exchange4", ExchangeType::DIRECT, true, false, std::unordered_map<std::string, std::string>());
    EXPECT_EQ(managerPtr->size(), 4);
}
TEST(ExchangeTest, selectExchangeTest)
{
    Exchange::Ptr exchangePtr = managerPtr->selectExchange("exchange2");
    EXPECT_EQ(exchangePtr->name, "exchange2");
    EXPECT_EQ(exchangePtr->type, ExchangeType::DIRECT);
    EXPECT_EQ(exchangePtr->durable, true);
    EXPECT_EQ(exchangePtr->autoDelete, false);
    // EXPECT_EQ(exchangePtr->serializeArgs(), "k1=v1;k2=v2;");
    // EXPECT_EQ(exchangePtr->serializeArgs(), "k2=v2;k1=v1;");        //这个变来变去的
}
TEST(ExchangeTest, deleteExchangeTest)
{
    managerPtr->deleteExchange("exchange3");
    EXPECT_EQ(managerPtr->size(), 3);

}
int main()
{
    testing::InitGoogleTest();
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    int ret = RUN_ALL_TESTS();
    return 0;
}